package com.lz.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <pre>
 * @描述：
 * @作者：lizhao
 * @创建时间： 2024/8/25 下午10:39
 * </pre>
 **/
@Configuration
public class InterceptorHandler implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        在目标方法（即Controller层的方法）执行前执行，返回值为是否放行。
        System.out.println("起床啦");
        return HandlerInterceptor.super.preHandle(request, response, handler);

    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//        目标方法执行后，进行运行
        System.out.println("睡觉1");
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
        System.out.println("睡觉2");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//        视图渲染完毕后，最后执行的方法。
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
        System.out.println("试图渲染完毕");
    }
}
